package gui.component;

import database.DatabaseWorker;
import database.entity.Contact;
import database.entity.User;
import gui.component.image.ResourceLoader;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.SoftBevelBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;

public class ContactChooser extends JPanel
{    
    User user;
    JPanel pnContactInfo;
    JPanel pnCombo;
    LinkedList<ContactInfo> contactInfos;
    AutoCompleteCombobox cboxFind;
           
    public ContactChooser(User user)
    {
        super();
        this.user=user;        
        initComponents();
    }
    
    private void initComponents()
    {
        setLayout(new FlowLayout(FlowLayout.TRAILING,0,0));
        contactInfos = new LinkedList<>();
        cboxFind = new AutoCompleteCombobox(this);        
        cboxFind.setEditable(true);
        cboxFind.setPreferredSize(new Dimension(250,25));
        pnContactInfo = new JPanel(new FlowLayout(FlowLayout.LEFT,5,0));
        pnCombo = new JPanel(new BorderLayout());
        pnCombo.add(cboxFind,BorderLayout.CENTER);
        
        //add listener so when we type in the combo box, we'll get the list of matched persons
        final JTextComponent tc = (JTextComponent) cboxFind.getEditor().getEditorComponent();
        tc.getDocument().addDocumentListener(new DocumentListener() 
        {
            @Override
            public void insertUpdate(DocumentEvent de) 
            {            
            }

            @Override
            public void removeUpdate(DocumentEvent de) 
            {            
            }

            @Override
            public void changedUpdate(DocumentEvent de) 
            {
                cboxFind.showPopup();
            }
        });
        
        //
        
        this.add(pnContactInfo);
        this.add(pnCombo);                 
    }
    
    public void addContact(String email)
    {
        Contact contact = user.databaseWorker.getContactFromEmail(DatabaseWorker.extractEmail(email));                            
        ContactInfo ci;
        if (contact==null)
        {
            ci = new ContactInfo(this, null, email, null);
        }
        else
        {
            ci = new ContactInfo(this, contact.ContactName, contact.Email, contact.PublicKey);
        }         
        contactInfos.add(ci);
        pnContactInfo.add(ci);
        revalidate();
    }
    
    void removeContactInfo(ContactInfo ci)
    {
        pnContactInfo.remove(ci);
        revalidate();
    }
    
    public boolean isEveryoneHasKey()
    {
        boolean result=true;
        for (ContactInfo contactInfo : contactInfos)
        {
            if (contactInfo.publicKey == null) 
            {
                result=false;
                break;
            }
        }
        return result;
    }
    
    public String getAllEmails()
    {
        if (contactInfos.isEmpty())
        {
            return null;
        }
        else
        {
            String result = "";
            for (int i = 0; i < contactInfos.size(); i++) 
            {
                result+=contactInfos.get(i).email;
                if (i<contactInfos.size()-1)
                    result+=",";
            }
            return result;
        }
    }
}

class ContactInfo extends JPanel
{
    ContactChooser parent;
    JLabel lbImgKey;
    JLabel lbEmail;
    JButton btRemove;
    ContactInfo _this;
    
    String name;
    String email;
    String publicKey;
    
    public ContactInfo(ContactChooser parent, String name, String email, String publicKey)
    {
        super();        
        this.parent=parent;        
        this.name=name;
        this.email=email;
        this.publicKey=publicKey;
        initComponents();        
    }   
    
    private void initComponents()
    {
        setLayout(new FlowLayout(FlowLayout.LEFT,2,0));
        lbImgKey = new JLabel();
        lbImgKey.setPreferredSize(new Dimension(15,15));
        if (publicKey==null)
        {
            lbImgKey.setVisible(false);
        }
        else
        {
            lbImgKey.setIcon(new ImageIcon(ResourceLoader.class.getResource("key.png")));
        }
        lbEmail = new JLabel();        
        if (name==null)
        {
            lbEmail.setText(email);
        }
        else
        {
            lbEmail.setText(name+" <"+email+">");
        }             
        btRemove = new JButton("x");
        btRemove.addActionListener((ActionEvent ae) -> 
        {
            parent.removeContactInfo(ContactInfo.this);
        });
        btRemove.setPreferredSize(new Dimension(35,20));
        this.add(lbImgKey);
        this.add(lbEmail);
        this.add(btRemove);
        setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
    }
}